package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Course;
import com.qk.mapper.CourseMapper;
import com.qk.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseServiseImpl implements CourseService {
    @Autowired
    private  CourseMapper courseMapper;

    @Override
    public void add(Course course) {
//        补全属性
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
//        调用Mapper方法
        courseMapper.insert(course);
    }

    @Override
    public PageResult<Course> list(Integer subject,String name,  Integer target, Integer page, Integer pageSize) {
        //设置PageHelper参数
        PageHelper.startPage(page,pageSize);
//        调用Mapper分页查询
        List<Course> courseList = courseMapper.selectList(subject,name, target);
//解析并封装结果分页
        Page<Course> p= (Page<Course>)courseList;

        return new PageResult<>(p.getTotal(),p.getResult());
    }

//    根据ID调用Mapper方法查询
    @Override
    public Course getById(Integer id) {
        return courseMapper.selectById(id);
    }

//    修改课程

    @Override
    public void update(Course course) {
//补全属性
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.update(course);
    }

//    根据ID删除课程

    @Override
    public void deleteById(Integer id) {
        courseMapper.deleteById(id);
    }
//    查询所有课程

    @Override
    public List<Course> selectAll() {
        return courseMapper.selectAll();

    }
//    根据学科查询课程

    @Override
    public List<Course> selectBySubject(Integer subject) {
        return courseMapper.selectBySubject(subject);
    }
}
